var ATOKEN = "781e67c8-1458-4e5f-8dbf-50973f2b58f7";
var UTOKEN = "bea06d67-2199-4ae8-8918-e0216cc463d7";
var CHANNEL_ID = 322;
var descriptionMaxLen = 700;
var imageScale = 0.6;
var articleLimit = 15;
var channelName = null;

function updateChannelName(channelID) {
    channelName = null;
    $.getJSON("http://server.newzsocial.com/newz/c/GetChannelInformation?aToken=" + ATOKEN + "&uToken=" + UTOKEN + "&channelId=" + channelID, function(receivedData) {
						channelName = receivedData.data.name;
						alert("channelName = " + channelName);
    });
}


function displayArticles(articles, channelID, channelName) {
		alert("displaying articles...");
		$("#articlesContainerTitle").html("Articles from channel " + channelID + ", " + channelName);
    for (var i = articles.length - 1; i >= 0; i--) {
        var article = articles[i];
        var imageURL = article.imageUrl;
        var imageHTML = "";
        if (imageURL) {
            imageHTML = "<IMG WIDTH=\"" + article.imageWidth * imageScale + "\" HEIGHT=\"" + article.imageHeight * imageScale + "\" SRC=\"" + imageURL + "\" ALIGN=LEFT>";
        }
        var articleDescription = article.description;
        if (articleDescription.length > descriptionMaxLen) {
            articleDescription = articleDescription.substring(0, descriptionMaxLen);
            articleDescription += "..."
        }

        $("#articlesContainerTitle").after("<p id=\"article" + i + "\">" + "<h3><a href=\"" + article.url + "\">" + article.title + " (id: " + article.id + ")</a></h3>" + imageHTML + "<b>" + article.publicationNames[0] + "</b> " + articleDescription + "<br clear=\"LEFT\"><br><br>" + "</p><hr>");
    }
}

function init() {
    var channelID = CHANNEL_ID;
    updateChannelName(channelID);
		$.getJSON("http://server.newzsocial.com/newz/c/GetArticlesForChannel?aToken=" + ATOKEN + "&uToken=" + UTOKEN + "&offset=0&limit=" + articleLimit + "&channelId=" + channelID, function(mainObj) {
        var articles = mainObj.data.articles;
        displayArticles(articles, channelID, channelName);
    });
}